package Module;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.io.File;

public class IntentModule extends ReactContextBaseJavaModule {

    private ReactApplicationContext context;

    public IntentModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.context = reactContext;
    }

    @Override
    public String getName() {
        return "IntentModule";
    }

    /*
     * 向ReactNative提供方法，用于跳转系统应用
     */
    @ReactMethod
    public void skipAction(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        Log.i("test_file", filePath);
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        String type;
        if (filePath.contains(".png") || filePath.contains(".jpg")) {
            type = "image/*";
        } else if (filePath.contains(".mp3") || filePath.contains(".mp4")) {
            type = "video/*";
        } else {
            type = "text/plain";
        }

        Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".FileProvider", new File(filePath));
        intent.setDataAndType(uri, type);
        context.startActivity(intent);
    }

}
